package com.wteam.dragon.model.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wteam.dragon.model.mapper.PictureMapper;
import com.wteam.dragon.model.pojo.bean.Picture;
import com.wteam.dragon.model.service.PictureService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.util.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2020/4/10 21:31
 */
@Service
@RequiredArgsConstructor
public class PictureServiceImpl implements PictureService {
    private final PictureMapper pictureMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addPicture(Picture picture) {
        pictureMapper.insert(picture);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("上传成功");
        return resultMessage;
    }

    @Override
    public ResultMessage listPicture(Integer type) {
        ValidationUtil.isNull("type", type);
        QueryWrapper<Picture> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Picture::getType, type);
        List<Picture> pictureList = pictureMapper.selectList(queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("pictureList", pictureList);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delPicture(Long id) {
        ValidationUtil.isNull("id", id);
        pictureMapper.deleteById(id);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }
}
